package com.wcy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcy.common.CommonResult;
import com.wcy.entity.Menu;
import com.wcy.entity.MenuRole;
import com.wcy.service.IMenuRoleService;
import com.wcy.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @since 2021-04-30
 */
@RestController
@RequestMapping("/menu")
public class MenuController {


    @Autowired
    IMenuService menuService;

    @Autowired
    IMenuRoleService menuRoleService;

    @RequestMapping("findMenuByUid")
    public List<Menu> findMenuByUid(Integer uid){
        List<Menu> onelev = menuService.findMenusByUid(uid, 1);
        System.out.println(onelev+"##########");
        return selectChild(uid,onelev);
    }

    private List<Menu> selectChild(Integer uid,List<Menu> oneL) {
        List<Menu> oneList = new ArrayList<>();
        //遍历一级菜单
        if(oneL!=null&& oneL.size()>0){
            oneL.forEach(oneMenu -> {
                System.out.println(oneMenu.getName()+"****");
                //根据一级的id作为二级的parentId查询二级菜单
                List<Menu> twoL = menuService.findMenusByUid(uid,oneMenu.getId());

                if(twoL!=null&& twoL.size()>0){
                    oneMenu.setChildren(twoL);
                    //递归调用
                    selectChild(uid,twoL);
                }
                oneList.add(oneMenu);
            });
        }

        /*if(oneL!=null && oneL.size()>0){
            oneL.forEach(oneMenu->{
                List<Menu> twoMenu = menuService.findMenusByUid(uid, oneMenu.getParentId());
                if(twoMenu!=null && twoMenu.size()>0){
                    //如果二级菜单不是空，就像查询到的二级菜单加到一级菜单下
                    oneMenu.setChildren(twoMenu);
                    //递归调用
                    selectChild(uid,twoMenu);
                }

                oneList.add(oneMenu);

            });
        }*/
        return oneList;
    }

    @RequestMapping("getTree")
    public CommonResult getTree(Integer roleId){
        HashMap<Object, Object> map = new HashMap<>();
        List<Menu> menuList= menuService.getTree(0);
        QueryWrapper wrapper = new QueryWrapper();
        QueryWrapper ww = new QueryWrapper();
        wrapper.eq("rid",roleId);
        List<MenuRole> list = menuRoleService.list(wrapper);
        List<Integer> mids = list.stream().map((mr) -> mr.getMid()).collect(Collectors.toList());
        List<Menu> menus = menuService.findMenusByIds(mids);
        List<Menu> collect1 = menus.stream().filter(menu -> menu.getLevel() != 1).collect(Collectors.toList());
        List<Integer> collect = collect1.stream().map(menu -> menu.getId()).collect(Collectors.toList());
        map.put("mids", collect);
        map.put("menuList", menuList);
        return CommonResult.success(map);

    }
    @RequestMapping("getMenuList")
    public List<Menu> getMenuList(String name){
        List<Menu> menuList = menuService.getMenuList(name);
        return menuList;

    }
    @RequestMapping("saveMenuRole")
    public boolean save(@RequestBody Map<String,Object> map){
        try {
            Integer rid = (Integer) map.get("rid");
            ArrayList<Integer> mids = (ArrayList<Integer>) map.get("mids");
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("rid", rid);
            menuRoleService.remove(wrapper);

            MenuRole menuRole = new MenuRole();
            for (Integer mid : mids) {
                if(mid!=1){
                    menuRole.setMid(mid);
                    menuRole.setRid(rid);
                    menuRoleService.save(menuRole);
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @RequestMapping("add")
    public boolean add(@RequestBody Menu menu){
        System.out.println(menu);
        int save =0;
        if (menu.getId()!=null){
            save = menuService.addMenu(menu);
        }else {
             save = menuService.updateMenu(menu);
        }

        if(save!=0){
            return true;
        }
        return false;
    }

    @RequestMapping("del")
    public boolean del(int id){
        boolean save = menuService.removeById(id);
        return save;
    }
    @RequestMapping("getMenu")
    public Menu getMenu(int id){
        Menu menu = menuService.getMenu(id);
        return menu;
    }
    @RequestMapping("update")
    public boolean update(Menu menuenu){
        int update = menuService.updateMenu(menuenu);
        if(update!=0){
            return true;
        }
        return false;

    }
}

